<template>
    <div class="chart-container">
        <div ref="chart" style="width: 100%; height:100%"></div>
    </div>
</template>

<script>
import * as echarts from 'echarts';
import 'echarts-gl';

export default {
    name: 'HelloWorld',
    data() {
        return {
            option: {},
            pieData: [
                {
                    name: "服务",
                    value: 18,
                    itemStyle: {
                        color: "#2A71FF",
                    },
                },
                {
                    name: "数据文件",
                    value: 1075,
                    itemStyle: {
                        color: "#00EDFE",
                    },
                },
                {
                    name: "数据库",
                    value: 38,
                    itemStyle: {
                        color: "#FEDB4B",
                    },
                },
               
            ],
        };
    },
    mounted() {
        this.initChart();
    },
    methods: {
        getParametricEquation(startRatio, endRatio, isSelected, isHovered, k, height) {
            let midRatio = (startRatio + endRatio) / 2;
            let startRadian = startRatio * Math.PI * 2;
            let endRadian = endRatio * Math.PI * 2;
            let midRadian = midRatio * Math.PI * 2;
            if (startRatio === 0 && endRatio === 1) {
                isSelected = false;
            }
            k = typeof k !== "undefined" ? k : 1 / 3;
            let offsetX = isSelected ? Math.cos(midRadian) * 0.1 : 0;
            let offsetY = isSelected ? Math.sin(midRadian) * 0.1 : 0;
            // let offsetZ = i == 1 ? 2 : 0;
            let hoverRate = isHovered ? 1.05 : 1;
            return {
                u: { min: -Math.PI, max: Math.PI * 3, step: Math.PI / 32 },
                v: { min: 0, max: Math.PI * 2, step: Math.PI / 20 },
                x: (u, v) => {
                    if (u < startRadian) {
                        return offsetX + Math.cos(startRadian) * (1 + Math.cos(v) * k) * hoverRate;
                    }
                    if (u > endRadian) {
                        return offsetX + Math.cos(endRadian) * (1 + Math.cos(v) * k) * hoverRate;
                    }
                    return offsetX + Math.cos(u) * (1 + Math.cos(v) * k) * hoverRate;
                },
                y: (u, v) => {
                    if (u < startRadian) {
                        return offsetY + Math.sin(startRadian) * (1 + Math.cos(v) * k) * hoverRate;
                    }
                    if (u > endRadian) {
                        return offsetY + Math.sin(endRadian) * (1 + Math.cos(v) * k) * hoverRate;
                    }
                    return offsetY + Math.sin(u) * (1 + Math.cos(v) * k) * hoverRate;
                },
                z: (u, v) => {
                    if (u < -Math.PI * 0.5) {
                        return Math.sin(u);
                    }
                    if (u > Math.PI * 2.5) {
                        return Math.sin(u);
                    }
                    return Math.sin(v) > 0 ? height : -1;
                },
            };
        },
        getPie3D(pieData, internalDiameterRatio) {
            let series = [];
            let sumValue = 0;
            let startValue = 0;
            let endValue = 0;
            let k = internalDiameterRatio !== undefined ? (1 - internalDiameterRatio) / (1 + internalDiameterRatio) : 1 / 3;
            for (let i = 0; i < pieData.length; i++) {
                sumValue += pieData[i].value;
                let seriesItem = {
                    name: pieData[i].name,
                    type: "surface",
                    parametric: true,
                    wireframe: { show: false },
                    pieData: pieData[i],
                    pieStatus: { selected: false, hovered: false, k: k },
                    label: {
                        show: true,
                        formatter: params => {
                            let value = params.data.pieData.value;
                            return ((value / sumValue) * 100).toFixed(2) + '%';
                        },
                        position: 'top',
                        color: '#fff',
                        fontSize: 14,
                        fontWeight: 'bold',
                        backgroundColor: 'rgba(0, 0, 0, 0.5)',
                        padding: [5, 10],
                        borderRadius: 4,
                    },
                    itemStyle: pieData[i].itemStyle,
                };
                series.push(seriesItem);
                endValue = startValue + pieData[i].value;
                seriesItem.parametricEquation = this.getParametricEquation(
                    startValue / sumValue,
                    endValue / sumValue,
                    false,
                    false,
                    k,
                    10,
                    i
                );
                startValue = endValue;
            }
            return series;
        },
        initChart() {
            const series = this.getPie3D(this.pieData, 0);
            series.push({
                name: "pie2d",
                type: "pie",
                label: {
                    opacity: 1,
                    fontSize: 14,
                    // lineHeight: 20,
                    textStyle: {
                        fontSize: 14,
                        color: "#fff",
                    },
                },
                labelLine: {
                    length: 30,
                    length2: 30,
                },
                startAngle: -30,
                clockwise: false,
                radius: ["40%", "60%"],
                center: ["50%", "50%"],
                data: this.pieData,
                itemStyle: {
                    opacity: 0,
                },
            });
            const option = {
                legend: {
                    show: true,
                    tooltip: { show: true },
                    orient: "vertical",
                    data: ["服务", "数据文件", "数据库"],
                    top: "center",
                    itemGap: 14,
                    itemHeight: 8,
                    itemWidth: 17,
                    right: "2%",
                    textStyle: { color: "#fff", fontSize: 12 },
                },
                animation: false,
                tooltip: {
                    formatter: (params) => {
                        if (params.seriesName !== "mouseoutSeries" && params.seriesName !== "pie2d") {
                            return `${params.seriesName}<br/><span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:${params.color};"></span>${option.series[params.seriesIndex].pieData.value + "人"}`;
                        }
                    },
                    textStyle: { fontSize: 14 },
                },
                // labelLine: {
                //     show: true,
                //     lineStyle: { color: "#7BC0CB" },
                //     normal: { show: true, length: 10, length2: 10 },
                // },
                label: {
                    show: true,
                    position: "inside",
                    formatter: "{b} \n{d}%",
                    textStyle: { color: "#fff", fontSize: "14px" },
                },
                xAxis3D: { min: -1, max: 1 },
                yAxis3D: { min: -1, max: 1 },
                zAxis3D: { min: -1, max: 1 },
                grid3D: {
                    show: false,
                    boxHeight: 2,
                    left: "4%",
                    bottom: "50%",
                    viewControl: { distance: 320, alpha: 30, beta: 20, autoRotate: false },
                },
                series: series,
            };
            const chartDom = this.$refs.chart;
            if (chartDom) {
                const myChart = echarts.init(chartDom);
                myChart.setOption(option);
            }
            this.option = option;
        },
    },
};
</script>

<style scoped>
.chart-container {
    width: 100%;
    height: 100%;
}
</style>